package main

import "fmt"

func menu(){
	fmt.Println("\t\t\t\t\t*****************")
	fmt.Println("\t\t\t\t\t学生成绩管理系统")
	fmt.Println("\t\t\t\t\t*****************")
	fmt.Println("1、新增一名学生")
	fmt.Println("2、按科目来输入每一名学生的单科成绩")
	fmt.Println("3、查询学生信息")
	fmt.Println("4、根据学号修改学生信息")
	fmt.Println("5、对学生成绩进行班级排名")
	fmt.Println("6、统计所有不及格科目超过2科的学生名单")
	fmt.Println("7、删除一名学生")
	fmt.Println("8、更改首个输入的学生和最后一个输入的学生的位置")
	fmt.Println("9、求所有学生的平均成绩排名")
	fmt.Println("10、退出系统")
	fmt.Println("请输入相应序号")
}

func student_in(stu_head *arrow) *arrow{
	loc := new(arrow)

	loc.Tnext = nil
	
	fmt.Println("\t\t\t请输入第一名学生的信息:")
	fmt.Println("\t\t\t姓名:")
	fmt.Scanf("%s",&loc.student_name)
	fmt.Println("\t\t\t性别:")
	fmt.Scanf("%s",&loc.student_se)
	fmt.Println("\t\t\t专业:")
	fmt.Scanf("%s",&loc.student_pro)
	fmt.Println("\t\t\t班级:")
	fmt.Scanf("%d",&loc.student_class)
	fmt.Println("\t\t\t学号:")
	fmt.Scanf("%d",&loc.student_num)
	loc.student_English = 999
	loc.student_Math = 999
	loc.student_C = 999
	loc.level = 0
	loc.average = 0

	stu_head = loc

	return stu_head

}

func student_in_add(stu_head *arrow){
	cod , nod:= stu_head , stu_head
	loc := new(arrow)

	loc.Tnext = nil
	var number_stu int = 1

	if stu_head == nil{
		fmt.Println("\n\t\t出错!")
		panic("crash")
	}

	for nod.Tnext != nil{
		number_stu++
		nod = nod.Tnext
	}

	fmt.Printf("已录入学生的个数%d",number_stu)

	fmt.Println("\n\n\t\t请输入姓名")
	fmt.Scanf("%s",&loc.student_name)
	fmt.Println("\t\t请输入性别")
	fmt.Scanf("%s",&loc.student_se)
	fmt.Println("\t\t请输入专业")
	fmt.Scanf("%s",&loc.student_pro)
	fmt.Println("\t\t请输入班级")
	fmt.Scanf("%s",&loc.student_class)
	fmt.Println("\t\t请输入学号")
	fmt.Scanf("%s",&loc.student_num)

	for cod != nil{
		if cod.student_num == loc.student_num {
			fmt.Println("\n\t\t输入的学号重复，请重新输入:")
			fmt.Scanf("%d",&loc.student_num)
		}
		cod = cod.Tnext
	}
	loc.student_English = 999
	loc.student_Math = 999
	loc.student_C = 999
	loc.level = 0
	loc.average = 0
	nod.Tnext = loc

}

func student_add(stu_head *arrow){
	var numt int = 0
	loc := stu_head
	fmt.Println("\t\t请选择科目进行输入:1、英语  2、高数  3、C语言  :")
	fmt.Scanf("%d",&numt)
	fmt.Print("\n\n\t\t姓名\t\t\t\t")
	
	switch numt {
	case 1: fmt.Print("英语成绩")
	        for loc != nil {
				fmt.Printf("\n\n\t\t%s\t\t\t\t",loc.student_name)
				if loc.student_English == 999{
					fmt.Scanf("%f",&loc.student_English)
				}else {
					fmt.Printf("%f",loc.student_English)
				}
				loc = loc.Tnext
			}
	case 2:fmt.Print("高数成绩")
	        for loc != nil {
		fmt.Printf("\n\n\t\t%s\t\t\t\t",loc.student_name)
		        if loc.student_Math == 999{
			        fmt.Scanf("%f",&loc.student_Math)
		        }else {
			        fmt.Printf("%f",loc.student_Math)
		    }
		        loc = loc.Tnext
			}
	case 3:fmt.Print("高数成绩")
	        for loc != nil {
        fmt.Printf("\n\n\t\t%s\t\t\t\t",loc.student_name)
		         if loc.student_Math == 999{
			        fmt.Scanf("%f",&loc.student_Math)
		        }else {
			        fmt.Printf("%f",loc.student_Math)
	        }
				loc = loc.Tnext
	}
	default:fmt.Print("\n\n\t\t输入错误")
}

}

func student_fin(stu_head *arrow){
	loc := stu_head
	var student_nu int = 0
	var sys int = 0

	fmt.Println("\t\t\t请输入学号以查找该学生:")

	fmt.Scanf("%d",&student_nu)

	for loc != nil {
		if student_nu == loc.student_num{
			sys++
			fmt.Printf("\n姓名:%s",loc.student_name)
			fmt.Printf("\n性别:%s",loc.student_se)
			fmt.Printf("\n专业:%s",loc.student_pro)
			fmt.Printf("\n班级:%d",loc.student_class)
			fmt.Printf("\n学号:%d",loc.student_num)
			if loc.student_English == 999 {
				fmt.Printf("\n该学生的英语成绩不存在")
			}else {
				fmt.Printf("\n该学生的英语成绩:%f",loc.student_English)
			}
			if loc.student_Math == 999 {
				fmt.Printf("\n该学生的高数成绩不存在")
			}else {
				fmt.Printf("\n该学生的高数成绩:%f",loc.student_Math)
			}
			if loc.student_C == 999 {
				fmt.Printf("\n该学生的C语言成绩不存在")
			}else {
				fmt.Printf("\n该学生的C语言成绩:%f",loc.student_C)
			}
		}
		loc = loc.Tnext
	}
	if sys == 0{
		fmt.Printf("\n未查找到该学生\n\n\n")
	}
}

func student_del(stu_head *arrow) *arrow{
	var choose int = 0
	var cod int = 0
	var tonumber int = 0
	loc := stu_head

	fmt.Printf("\n\n\t\t请输入要修改信息的学生的学号:")
	fmt.Scanf("%d",&tonumber)

	for loc != nil{
		if tonumber == loc.student_num {
			fmt.Printf("\n\n\t请选择要更改的信息:1、姓名  2、性别  3、专业名称  4、班级  5、学号")
			fmt.Printf("\n\t6、英语成绩  7、高数成绩  8、C语言成绩\n")
			fmt.Scanf("%d",&choose)
			if choose == 1{
				fmt.Printf("\n\n\t请输入修改后的姓名:")
				fmt.Scanf("%s",loc.student_name)
				fmt.Printf("\n\n\t\t修改完成!")
				cod++
			}else if choose == 2 {
				fmt.Printf("\n\n\t请输入修改后的性别:")
				fmt.Scanf("%s",loc.student_se)
				fmt.Printf("\n\n\t\t修改完成!")
				cod++
			}else if choose == 3 {
				fmt.Printf("\n\n\t请输入修改后的专业名称:")
				fmt.Scanf("%s",loc.student_pro)
				fmt.Printf("\n\n\t\t修改完成!")
				cod++
			}else if choose == 4 {
				fmt.Printf("\n\n\t请输入修改后的班级:")
				fmt.Scanf("%s",loc.student_class)
				fmt.Printf("\n\n\t\t修改完成!")
				cod++
			}else if choose == 5 {
				fmt.Printf("\n\n\t请输入修改后的学号:")
				fmt.Scanf("%s",loc.student_num)
				fmt.Printf("\n\n\t\t修改完成!")
				cod++
			}else if choose == 6 {
				fmt.Printf("\n\n\t请输入修改后的英语成绩:")
				fmt.Scanf("%s",loc.student_English)
				fmt.Printf("\n\n\t\t修改完成!")
				cod++
			}else if choose == 7 {
				fmt.Printf("\n\n\t请输入修改后的高数成绩:")
				fmt.Scanf("%s",loc.student_Math)
				fmt.Printf("\n\n\t\t修改完成!")
				cod++
			}else if choose == 8 {
				fmt.Printf("\n\n\t请输入修改后的C语言成绩:")
				fmt.Scanf("%s",loc.student_C)
				fmt.Printf("\n\n\t\t修改完成!")
				cod++
			}else {
				fmt.Printf("\n\n\t输入错误")
				cod++
			}
		}
		loc = loc.Tnext
	}

	if loc == nil && cod == 0{
		fmt.Println("输入的学号不存在")
	}
	return stu_head
}

func student_rank(stu_head *arrow){
	loc := stu_head
	var bata [2000]float64
	var cons float64 = 0
	var mun int = 0
	var sort int = 0
	var stu_cla int = 0
	var total int = 0
	var change int = 0
	var compare int = 0
	var pron string
	var judge int = 0

	fmt.Printf("\n\n\t\t请输入专业:")
	fmt.Scanf("%s",pron)
	for loc != nil {
		if pron == loc.student_pro {
			judge++
		}
		loc = loc.Tnext
	}
	loc = stu_head

	if judge != 0 {
		fmt.Printf("\n\n\t\t请输入班号:")
		fmt.Scanf("%d",&stu_cla)
		for loc != nil {
			if stu_cla == loc.student_class && pron == loc.student_pro{
				sort++
			}
				loc = loc.Tnext
		}
		loc =stu_head
		fmt.Printf("\n\n\t\t请输入需要进行排名的科目\n\t\t1、英语  2、高数  3、C语言\n")
		fmt.Scanf("%d",&mun)

		if mun == 1 && sort != 0 {
			fmt.Printf("\n\n\t\t姓名\t\t\t英语成绩")
			for loc != nil {
				if loc.student_class == stu_cla && pron == loc.student_pro {
					bata[total] = loc.student_English
					total++
				}
				loc = loc.Tnext
			}
			loc = stu_head

			for change = 0;change < sort;change++ {
				for compare = 0;compare < sort - change;compare++{
					if bata[compare] < bata[compare+1] {
						cons = bata[compare]
						bata[compare] = bata[compare+1]
						bata[compare+1] = cons
					}
				}
			}

			for change = 0;change <= sort;change++ {
				for loc != nil {
					if bata[change] == loc.student_English && loc.level != 22 {
						fmt.Printf("\n\n\t\t%s\t\t\t%f",loc.student_name,loc.student_English)
						loc.level = 22
						continue
					}
					loc = loc.Tnext
				}
				loc = stu_head
			}
		}else if mun == 2 && sort != 0 {
			fmt.Printf("\n\n\t\t姓名\t\t\t高数成绩")
			for loc != nil {
				if loc.student_class == stu_cla && pron == loc.student_pro {
					bata[total] = loc.student_Math
					total++
				}
				loc = loc.Tnext
			}
			loc = stu_head

			for change = 0;change < sort;change++ {
				for compare = 0;compare < sort - change;compare++{
					if bata[compare] < bata[compare+1] {
						cons = bata[compare]
						bata[compare] = bata[compare+1]
						bata[compare+1] = cons
					}
				}
			}

			for change = 0;change <= sort;change++ {
				for loc != nil {
					if bata[change] == loc.student_Math && loc.level != 22 {
						fmt.Printf("\n\n\t\t%s\t\t\t%f",loc.student_name,loc.student_Math)
						loc.level = 22
						continue
					}
					loc = loc.Tnext
				}
				loc = stu_head
			}
		}else if mun == 2 && sort != 0 {
			fmt.Printf("\n\n\t\t姓名\t\t\tC语言成绩")
			for loc != nil {
				if loc.student_class == stu_cla && pron == loc.student_pro {
					bata[total] = loc.student_C
					total++
				}
				loc = loc.Tnext
			}
			loc = stu_head

			for change = 0;change < sort;change++ {
				for compare = 0;compare < sort - change;compare++{
					if bata[compare] < bata[compare+1] {
						cons = bata[compare]
						bata[compare] = bata[compare+1]
						bata[compare+1] = cons
					}
				}
			}

			for change = 0;change <= sort;change++ {
				for loc != nil {
					if bata[change] == loc.student_C && loc.level != 22 {
						fmt.Printf("\n\n\t\t%s\t\t\t%f",loc.student_name,loc.student_C)
						loc.level = 22
						continue
					}
					loc = loc.Tnext
				}
				loc = stu_head
			}
		}else {
			fmt.Printf("\n\n\t\t输入数字错误")
		}
	}else {
		fmt.Printf("\n\n\t\t输入的专业不存在")
	}
	loc = stu_head
	for loc != nil {
		loc.level = 0
		loc = loc.Tnext
	}
}

func student_sta(stu_head *arrow){
	loc := stu_head
	fmt.Printf("\n\n2科及以上科目不及格者姓名")
	for loc != nil {
		switch {
		case 0<=loc.student_English && loc.student_English<60 && 0<=loc.student_Math && loc.student_Math<60: 
			fmt.Printf("\n\t%s\n",loc.student_name)
		case 0<=loc.student_English && loc.student_English<60 && 0<=loc.student_C && loc.student_C<60:
			fmt.Printf("\n\t%s\n",loc.student_name)
		case 0<=loc.student_C && loc.student_C<60 && 0<=loc.student_Math && loc.student_Math<60:
			fmt.Printf("\n\t%s\n",loc.student_name)
		} 
		loc = stu_head
	}
}

func student_in_del(stu_head *arrow){
	cod := stu_head
	nod := stu_head
	loc := stu_head
	var score int = 0
	var ium int = 0
	var up int = 0

	fmt.Printf("\n\n\n\t\t请输入要删除的学生的学号")
	fmt.Scanf("%d",&score)

	for nod != nil {
		up++
		nod = nod.Tnext
	}

	nod = stu_head

	if up != 1 && up != 0 {
		for loc != nil {
			if score == loc.student_num{
				fmt.Printf("\n\n\t\t即将要删除的学生的名字%s\n\n\n",loc.student_name)
				cod.Tnext = loc
				nod = loc.Tnext
				nod = cod.Tnext
				fmt.Printf("\n\n\t\t已删除该学生")
				ium++
				break
			}
			loc = loc.Tnext
		}
	}else {
		fmt.Printf("\n\n\t\t系统中的人数不足以删除")
	}
	if ium == 0 {
		fmt.Printf("\n\n\n\t\t输入的学号不存在")
	}
}

func student_change(stu_head *arrow) *arrow{
	loc := stu_head
	nod := stu_head

	if loc == nil {
		fmt.Printf("\n\n链表无数据，错误")
		panic("crash")
	}
	loc = stu_head

	for loc.Tnext.Tnext != nil {
		loc = loc.Tnext
	}

	nod = loc.Tnext
	nod.Tnext = stu_head.Tnext
	loc.Tnext = stu_head
	stu_head.Tnext = nil
	stu_head = nod

	return stu_head
}

func student_aver(stu_head *arrow){
	loc := stu_head
	var sort int = 0
	var brn int = 0
	var stt int = 0
	var rank [2000]int
	var change int = 0

	if loc == nil {
		fmt.Printf("\n\n\t\t程序异常")
		panic("crash")
	}

	fmt.Printf("\n\n\t\t姓名\t\t\t平均成绩\n\n")

	for loc != nil {
		loc.average = (int(loc.student_English)+int(loc.student_Math)+int(loc.student_C))/3
		rank[sort] = loc.average
		sort++
		loc = loc.Tnext
	}
	loc = stu_head

	brn = sort

	for sort = 0;sort <= brn;sort++ {
		for stt = 0;stt < brn - sort;stt++ {
			if rank[stt] < rank[stt+1] {
				change = rank[stt]
				rank[stt] = rank[stt+1]
				rank[stt+1] = change
			}
		}
	}

	for sort = 0;sort <= brn;sort++ {
		for loc != nil {
			if loc.average == rank[sort] && loc.level != 21 {
				fmt.Printf("\t\t%s\t\t\t%d\n",loc.student_name,loc.average)
				loc.level = 21
				continue
			}
			loc = loc.Tnext
		}
		loc = stu_head
	}

	loc = stu_head

	for loc != nil {
		loc.level = 0
		loc = loc.Tnext
	}
}